<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Address_model extends MY_Model
{
    public function __construct() {
        parent::__construct();
    }
	/**
	 * 获取列表
	 *
	 * return array
	 */
	public function getProvinceList($fields = " * ")
	{
    	$sql = "SELECT {$fields} FROM blues_region  WHERE type=1  AND pid = 0 ORDER BY id";
        $query = $this->db->query($sql);

		return $query->result_array();
	}


	public function getCityList($id, $fields = " * ")
	{
    	$sql = "SELECT {$fields} FROM blues_region  WHERE pid = {$id} ORDER BY id";
        $query = $this->db->query($sql);

		return $query->result_array();
	}

	public function getRegionById($id, $fields = " * "){
		$sql = "SELECT {$fields} FROM blues_region  WHERE id = ".$id;
        $query = $this->db->query($sql);

		return $query->row_array();
	}

	 public function address_list($AccountID, $fields = " * "){
        $sql = "SELECT {$fields} FROM yy_address WHERE AccountID = '{$AccountID}' ORDER BY isDefault";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function address_set_default($id, $AccountID){
    	$this->db->where(array("AccountID"=>$AccountID,"isDefault"=>1));
    	$this->db->update('yy_address',array('isDefault'=>0));

    	$this->db->where("id", $id);
    	$res = $this->db->update('yy_address', array('isDefault'=>1));
    	return $res;
    }

    public function address_get_default($AccountID, $fields = " * "){
    	$sql = "SELECT {$fields} FROM yy_address WHERE AccountID = '{$AccountID}' AND  isDefault = 1";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    public function address_detail($id, $fields = " * "){
    	$sql = "SELECT {$fields} FROM yy_address WHERE id = {$id}";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    public function address_add($data,$AccountID){
        if($data['isDefault'] == 1){
            $this->db->where(array("AccountID"=>$AccountID,"isDefault"=>1));
            $this->db->update('yy_address',array('isDefault'=>0));
        }
    	$this->db->insert("yy_address",$data);
    	
    	return $this->db->insert_id();
    }

    public function address_edit($id, $data){
        if($data['isDefault'] == 1){
            $this->db->where(array("AccountID"=>$data['AccountID'],"isDefault"=>1));
            $this->db->update('yy_address',array('isDefault'=>0));
        }
    	$this->db->where("id",$id);
    	$res = $this->db->update("yy_address",$data);
    	return $res;
    }

    public function address_del($id,$AccountID){
	    
    	$sql = "SELECT * FROM yy_address WHERE id = {$id}";
        $query = $this->db->query($sql);
	$defaultInfo = $query->row_array();
	$this->db->where("id", $id);
        $res = $this->db->delete("yy_address");
	if($defaultInfo['isdefault']==1){
		$sqllist = "SELECT id FROM yy_address WHERE AccountID = '{$AccountID}' ORDER BY id";
        	$listquery = $this->db->query($sqllist);
		$listArr = $listquery->result_array($listquery);
		if($listArr){
			$this->db->where("id", $listArr[0]['id']);
        		$res = $this->db->update('yy_address',array('isDefault'=>1));			
		}		
	}
	return $res;
    }

}

